package com.github.leecho.ssc.captcha.authentication;

import com.github.leecho.ssc.captcha.repository.CaptchaRepository;
import org.springframework.security.authentication.AuthenticationDetailsSource;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LIQIU
 * created on 2020/11/13
 **/
public class CaptchaAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, CaptchaAuthenticationDetails> {

    private final CaptchaRepository<HttpServletRequest> captchaRepository;

    public CaptchaAuthenticationDetailsSource(CaptchaRepository<HttpServletRequest> captchaRepository) {
        this.captchaRepository = captchaRepository;
    }

    @Override
    public CaptchaAuthenticationDetails buildDetails(HttpServletRequest httpServletRequest) {
        CaptchaAuthenticationDetails captchaAuthenticationDetails = new CaptchaAuthenticationDetails(httpServletRequest);
        captchaAuthenticationDetails.setPresetCaptcha(captchaRepository.load(httpServletRequest));
        return captchaAuthenticationDetails;
    }
}
